<template>
  <el-dialog
    :title="!dataForm.lifegrowthId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="55%"
    top="5vh"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="20px">
      <el-form-item prop="content">
        <quill-editor
          ref="myQuillEditor"
          v-model="dataForm.content"
          :options="editorOption"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import reqApi from '@/api/life'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        lifegrowthId: '',
        content: '',
        writeTime: ''
      },
      dataRule: {
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        writeTime: [
          { required: true, message: '撰写时间不能为空', trigger: 'blur' }
        ]
      },
      editorOption: {}
    }
  },
  methods: {
    init(id) {
      this.dataForm.lifegrowthId = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.lifegrowthId) {
          reqApi.getInfo(this.dataForm.lifegrowthId).then(({ data }) => {
            if (data) {
              this.dataForm.content = data.lifeGrowth.content
              this.dataForm.writeTime = data.lifeGrowth.writeTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      // this.getDate()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (!this.dataForm.lifegrowthId) {
            reqApi.save(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            reqApi.update(this.dataForm).then(({ data }) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        }
      })
    }
    // },
    // getDate() {
    //   var aData = new Date()
    //   this.dataForm.writeTime = aData.getFullYear() + '-' + (aData.getMonth() + 1) + '-' + aData.getDate()
    //   console.log(this.dataForm.writeTime)
    // }
  }
}
</script>

<style>
.ql-editor{
    min-height: 350px;
    max-height: 350px;
}
</style>
